#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/soundcard.h>

#define BUF_SIZE 4096

main (int ac, char **av)
{
  int fd_file, fd_audio, n, nleft, nwritten;
  char buf[BUF_SIZE], *p;

  if ((fd_file = open (av[1], O_RDONLY)) < 0) {
    perror (av[1]);
    exit (1);
  }

  if ((fd_audio = open ("/dev/audio", O_WRONLY)) < 0) {
    perror ("/dev/audio");
    exit (1);
  }

  /* Lecture de l'échantillon */
  while ((n = read (fd_file, buf, sizeof(buf))) > 0) {
    nleft = n;
    p = buf;

    /* On envoit l'échantillon... */
    while (nleft) {
      if ((nwritten = write (fd_audio, p, nleft)) < 0)
	perror ("/dev/audio");
      else
	printf ("%d/%d written.\n", nwritten, nleft);

      nleft -= nwritten;
      p += nwritten;
    }
  }

  close (fd_file);
  close (fd_audio);
}

